package com.seal.yoyoshop.web;

import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.bin.User;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.UserService;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;
import com.seal.yoyoshop.service.impl.UserServiceImpl;
import com.seal.yoyoshop.util.SafeUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "MyCenterServlet", value = "/index/my")
public class MyCenterServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
            response.sendRedirect("login");
        }else {
            request.setAttribute("flag",4);
            List<Type> typeList = typeService.getTypeList();
            request.setAttribute("typeList",typeList);

            request.getRequestDispatcher("/index/my.jsp").forward(request,response);
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
            response.sendRedirect("login");
        }else {
            String userIdstr = request.getParameter("id");
            Integer userId = Integer.parseInt(userIdstr);
            String typestr = request.getParameter("type");
            Integer type = Integer.parseInt(typestr);
            if (type == 1) {
                request.setCharacterEncoding("UTF-8");
                String name = request.getParameter("name");
                String phone = request.getParameter("phone");
                String address = request.getParameter("address");
                user.setName(name);
                user.setPhone(phone);
                user.setAddress(address);
                boolean issuccess = userService.updateUser(user);
                if (issuccess){
                    request.setAttribute("msg","个人信息修改成功");
                }else{
                    request.setAttribute("msg","个人信息修改失败姓名或住址过长");
                }
            }else {
                String password = request.getParameter("password");
                String passwordNew = request.getParameter("passwordNew");
//                User finduser = userService.getUserById(userId);
                String oldPwd = SafeUtil.encode(password);
                String currentPwd = user.getPassword();
                if(currentPwd.equals(oldPwd)){
                    user.setPassword(SafeUtil.encode(passwordNew));
                    userService.updateUserPassword(user.getUsername(),passwordNew);
                    request.setAttribute("msg","密码修改成功");
                }else {
                    request.setAttribute("msg","密码修改失败，原密码输入有误");
                }


            }

            request.setAttribute("flag",4);
            List<Type> typeList = typeService.getTypeList();
            request.setAttribute("typeList",typeList);

            request.getRequestDispatcher("/index/my.jsp").forward(request,response);
        }
    }
}
